package com.yanq.liancar.activity;
/*
 *  @创建者     Administrator
 *  @创建时间   2018/9/26  20:15
 *  @描述       ${TODO}
 */


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.nex3z.flowlayout.FlowLayout;
import com.vondear.rxtool.view.RxToast;
import com.yanq.liancar.R;
import com.yanq.liancar.activity.base.BaseToolbarActivity;
import com.yanq.liancar.bean.BaseBean;
import com.yanq.liancar.bean.UserBean;
import com.yanq.liancar.url.Config;
import com.yanq.liancar.util.LogUtils;
import com.yanq.liancar.util.Utils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ShopEvaluationActivity extends BaseToolbarActivity {
    @BindView(R.id.et_content)
    EditText etContent;
    @BindView(R.id.img_statu)
    ImageView imgStatu;
    @BindView(R.id.ll_niming)
    LinearLayout llNiming;
    @BindView(R.id.Release)
    TextView Release;
    @BindView(R.id.ratingbar)
    RatingBar ratingbar;
    @BindView(R.id.flow)
    FlowLayout flow;

    private String orderNum;
    private float stars;
    private String[] mStrings1 = {"服务态度差", "效率低下", "设施设备不齐全", "收费不合理", "操作不熟练", "店铺形象不好", "未使用指定商品", "配件使用次品", "返修率高"};
    private String[] mStrings2 = {"技术可靠", "价格合理", "干净整洁", "效率高效", "态度良好"};
    //private List<String> mStringList=new ArrayList<>();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shopevaluation);
        ButterKnife.bind(this);
        Utils.setStatusBar(this, true, false);
        mTvTitle.setText("服务评价");
        orderNum = getIntent().getExtras().getString("orderId");
        initView();
    }

    private void initView() {

        ratingbar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {
                LogUtils.e("sadadadad", v + "");
                stars = v;
                flow.removeAllViews();
                if (v < 4.5) {
                    for (int i = 0; i < mStrings1.length; i++) {
                        TextView textView = buildLabel(mStrings1[i]);
                        flow.addView(textView);
                    }
                }else {
                    for (int i = 0; i <mStrings2.length ; i++) {
                        TextView textView = buildLabel(mStrings2[i]);
                        flow.addView(textView);
                    }
                }
            }
        });

    }

    @OnClick(R.id.Release)
    public void onViewClicked() {
        if (!TextUtils.isEmpty(etContent.getText().toString().trim())) {
            setEvaluation();
        } else {
            RxToast.error("请输入评价内容");
        }
    }

    private void setEvaluation() {
        OkGo.<String>post(Config.s + "llc/remarkCarService")
                .tag(this)
                .params("token", UserBean.getcToken(this))
                .params("content", etContent.getText().toString().trim())
                .params("grade", stars + "")
                .params("onum", orderNum)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        BaseBean baseBean = gson.fromJson(response.body(), BaseBean.class);
                        if (baseBean.isIsSuccess()) {
                            RxToast.success("评价成功");
                            finish();
                        } else {
                            RxToast.error(baseBean.getOutputMessage());
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        RxToast.error("服务器连接失败");
                        super.onError(response);
                    }
                });
    }

    private TextView buildLabel(final String text) {
        final TextView textView = new TextView(this);
        textView.setText(text);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13);
        textView.setPadding((int) dpToPx(16), (int) dpToPx(6), (int) dpToPx(16), (int) dpToPx(6));
        textView.setBackgroundResource(R.drawable.selector_tv_bg_a);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //et.setText(text);
                textView.setBackgroundResource(R.drawable.selector_tv_bg_b);
                textView.setTextColor(getResources().getColor(R.color.white));
//                mStringList.add(textView.toString());
//
//                LogUtils.e("adafafasdasfaohfa;",mStringList.size()+"");
            }
        });
        return textView;
    }

    private float dpToPx(float dp) {
        return TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics());
    }

}
